package com.ny.shiyan.shiyan5;

import java.util.List;
import java.util.Scanner;

public class EmployeeManagementSystem {
    private static Scanner scanner = new Scanner(System.in);
    private static EmployeeManager manager = new EmployeeManager();

    public static void main(String[] args) {
        initializeSampleData();

        while (true) {
            System.out.println("\n===== 员工信息管理系统 =====");
            System.out.println("1. 添加员工");
            System.out.println("2. 修改员工信息");
            System.out.println("3. 删除员工");
            System.out.println("4. 查询员工");
            System.out.println("5. 退出系统");
            System.out.print("请选择操作: ");

            int choice = scanner.nextInt();
            scanner.nextLine();

            switch (choice) {
                case 1:
                    addEmployee();
                    break;
                case 2:
                    updateEmployee();
                    break;
                case 3:
                    deleteEmployee();
                    break;
                case 4:
                    displayAllEmployees();
                    break;
                case 5:
                    System.out.println("感谢使用，再见！");
                    return;
                default:
                    System.out.println("无效选项，请重新输入！");
            }
        }
    }

    private static void initializeSampleData() {
        manager.addEmployee(new Employee("张三", 28, 15000));
        manager.addEmployee(new Employee("李四", 35, 25000));
        manager.addEmployee(new Employee("王五", 42, 32000));
    }

    private static void addEmployee() {
        System.out.println("\n--- 添加新员工 ---");
        System.out.print("请输入员工姓名: ");
        String name = scanner.nextLine();

        System.out.print("请输入员工年龄: ");
        int age = scanner.nextInt();

        System.out.print("请输入员工薪资: ");
        double salary = scanner.nextDouble();
        scanner.nextLine();

        manager.addEmployee(new Employee(name, age, salary));
    }

    private static void updateEmployee() {
        System.out.println("\n--- 修改员工信息 ---");
        System.out.print("请输入要修改的员工姓名: ");
        String name = scanner.nextLine();

        Employee existing = manager.findEmployee(name);
        if (existing == null) {
            System.out.println("员工不存在: " + name);
            return;
        }

        System.out.println("当前信息: " + existing);

        System.out.print("请输入新姓名 (直接回车保持不变): ");
        String newName = scanner.nextLine();
        if (newName.isEmpty()) newName = existing.getName();

        System.out.print("请输入新年龄 (直接回车保持不变): ");
        String ageInput = scanner.nextLine();
        int newAge = ageInput.isEmpty() ? existing.getAge() : Integer.parseInt(ageInput);

        System.out.print("请输入新薪资 (直接回车保持不变): ");
        String salaryInput = scanner.nextLine();
        double newSalary = salaryInput.isEmpty() ? existing.getSalary() : Double.parseDouble(salaryInput);

        manager.updateEmployee(name, new Employee(newName, newAge, newSalary));
    }

    private static void deleteEmployee() {
        System.out.println("\n--- 删除员工 ---");
        System.out.print("请输入要删除的员工姓名: ");
        String name = scanner.nextLine();

        manager.deleteEmployee(name);
    }


    private static void displayAllEmployees() {
        System.out.println("\n--- 所有员工信息 ---");
        List<Employee> allEmployees = manager.getAllEmployees();

        if (allEmployees.isEmpty()) {
            System.out.println("没有员工记录");
            return;
        }

        System.out.println("        员工列表        ");

        for (Employee emp : allEmployees) {
            System.out.println(emp);
        }

        System.out.println("总计: " + allEmployees.size() + " 名员工");
    }
}